package com.clkj.cldisabledmanclient.adapter.jaylv;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.clkj.cldisabledmanclient.R;


public class ViewHolder {
    private final SparseArray<View> mViews;
    private int mPosition;
    private Context mContext;
    private View mConvertView;

    private ViewHolder(Context context, ViewGroup parent, int layoutId,
                       int position) {
        this.mPosition = position;
        this.mContext = context;
        this.mViews = new SparseArray<View>();
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,
                false);
        mConvertView.setTag(this);
    }

    /**
     * get one ViewHolder object
     *
     * @param context
     * @param convertView
     * @param parent
     * @param layoutId
     * @param position
     * @return
     */
    public static ViewHolder get(Context context, View convertView,
                                 ViewGroup parent, int layoutId, int position) {

        if (convertView == null) {
            return new ViewHolder(context, parent, layoutId, position);
        }
        return (ViewHolder) convertView.getTag();
    }

    /**
     * get some view by its id;if not exist,then add to array
     *
     * @param viewId
     * @return
     */
    public <T extends View> T getView(int viewId) {

        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    public View getConvertView() {
        return mConvertView;
    }

    /**
     * set text to TextView
     *
     * @param viewId
     * @param text
     * @return
     */
    public ViewHolder setText(int viewId, String text) {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }

    /**
     * set image to ImageView by drawableId
     *
     * @param viewId
     * @param drawableId
     * @return
     */
    public ViewHolder setImageResource(int viewId, int drawableId) {
        ImageView view = getView(viewId);
        view.setImageResource(drawableId);

        return this;
    }

    /**
     * set image to ImageView by Bitmap
     *
     * @param viewId
     * @param
     * @return
     */
    public ViewHolder setImageBitmap(int viewId, Bitmap bm) {
        ImageView view = getView(viewId);
        view.setImageBitmap(bm);
        return this;
    }

    /**
     * set image to ImageView by url
     *
     * @param viewId
     * @param
     * @return
     */
    public ViewHolder setImageByUrl(int viewId, String url) {

        if (TextUtils.isEmpty(url)) {


            Glide.with(mContext).load(R.mipmap.ico_data_empty)
                    .into((ImageView) getView(viewId));

        } else {
            RequestOptions options = new RequestOptions().error(R.mipmap.ico_data_empty)
                    .placeholder(R.mipmap.ico_data_empty);
            Glide.with(mContext).load(url)
                    .apply(options)
                    .into((ImageView) getView(viewId));
        }

        return this;
    }

    public int getPosition() {
        return mPosition;
    }
}
